#include "Wimp.h"
#include "Desklib:Event.h"
#include "Desklib:Error.h"
#include "Desklib:Window.h"
#include "Desklib:Resource.h"
#include "Desklib:WimpSwis.h"
#include "Desklib:EventMsg.h"
#include "Desklib:icon.h"
#include "Desklib:screen.h"
#include "Desklib:Pointer.h"
#include "Desklib:Window.h"
#include "Desklib:Template.h"
#include "Desklib:Menu.h"
#include "Desklib:Handler.h"
static sprite_area usersp;
static BOOL quit=FALSE;
static window_handle info_window;
static menu_ptr main_menu;

/*handles clicking on the icon bar icon*/
static BOOL BarClick(event_pollblock *e, void *r){
  if (e->data.mouse.button.data.menu){
    Menu_Show(main_menu,e->data.mouse.pos.x,224);
  }
  return 1;
}

static BOOL load(event_pollblock*e,void *r);

static BOOL Click(event_pollblock *e, void *r){
  strcpy(e->data.message.data.dataload.filename,"<SesAnimate$Dir>.Gif");
  load(e,r);
  return 1;
}

/*handles the icon bar menu*/
static BOOL MainMenu(event_pollblock *e, void *r){
  int g,h;
  switch (e->data.words[0]){
    case 1 :
      Menu_GetFlags(main_menu,1,&g,&h);
      Menu_SetFlags(main_menu,1,1-g,h);
      break;
    case 2 :quit=TRUE;break;
  }
  return 1;
}

static BOOL save(event_pollblock * e ,void * r)
{
  message_block message;
  message.header.size=256;
  message.header.yourref=e->data.message.header.myref;
  message.header.action=message_DATASAVEACK;
  message.data.datasaveack.icon=e->data.message.data.datasave.icon;
  message.data.datasaveack.pos=e->data.message.data.datasave.pos;
  message.data.datasaveack.estsize=e->data.message.data.datasave.estsize;
  message.data.datasaveack.filetype=e->data.message.data.datasave.filetype;
  strcpy(message.data.datasaveack.filename,"<Wimp$ScrapDir>.Sprite!");
  Wimp_SendMessage(18,&message,e->data.message.header.sender,0);
  return 1;
}

BOOL CheckLoaded(void){
 char buffer[512];
  if(!OS_ReadVarVal("Alias$InterGif",buffer,512)){
    buffer[0]=0;
    buffer[1]=0;
    buffer[2]=0;
    buffer[3]=0;
    strcpy(buffer+4,"Please find InterGif first, then try again. Or, switch off buffering\0");
    SWI(6,0,0x400DF,buffer,256,"SES Animater","!sesanim",1,"I'll try");
    return 0;
  }else{
    return 1;
  }
}

static BOOL load(event_pollblock * e ,void * r)
{
  char text[512];
  int g,h;
  Menu_GetFlags(main_menu,1,&g,&h);
  switch(g){
    case 0 :sprintf(text,"<SesAnimate$Dir>.RunAnim %s",e->data.message.data.dataload.filename);Wimp_StartTask(text);break;
    case 1 :if(CheckLoaded()){
            sprintf(text,"InterGif -s -o <Wimp$Scrap> %s ",e->data.message.data.dataload.filename);
            Wimp_StartTask(text);
            Wimp_StartTask("<SesAnimate$Dir>.RunBuff <Wimp$Scrap>");
            break;
    }
  }
}


void main(void){
  icon_handle baricon;
  event_pollblock event;
  event_pollmask mask;
  char appname[64] = "Animate";
  Resource_Initialise("SESAnimate");
  main_menu=Menu_New("Animate","Info,Buffered,Quit");
  usersp = Sprite_LoadFile("<SESAnimate$Dir>.FrontSpri");
  Event_Initialise(appname);
  EventMsg_Initialise();
  Template_LoadFile("FrontTemp");
  Template_UseSpriteArea(usersp);
  info_window=Window_Create("Info",80);
  Menu_AddSubMenu(main_menu,0,(menu_ptr) info_window);
  baricon= Icon_BarIcon("!sesanim",iconbar_RIGHT);
  EventMsg_Claim(message_DATALOAD,event_ANY,load,NULL);
  Event_Claim(event_CLICK,window_ICONBAR,event_ANY,BarClick,NULL);
  Event_Claim(event_CLICK,info_window,event_ANY,Click,NULL);
  Event_Claim(event_OPEN,event_ANY,event_ANY,Handler_OpenWindow,NULL);
  Event_Claim(event_MENU,event_ANY,event_ANY,MainMenu,NULL);
  EventMsg_Claim(message_DATASAVE,event_ANY,save,NULL);
  while(!quit){
    mask.value=1;
    Wimp_Poll(mask,&event);
    Event_Process(&event);
  }
}
